package com.settlement.system.controller;

import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.BasePager;
import com.settlement.system.model.entity.common.TblWpAccountConfig;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblWpAccountConfigService;
import com.settlement.system.common.util.common.FasUtil;
import com.alibaba.excel.context.AnalysisContext;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * WP-账号配置 前端控制器
 * @author Ximi
 * @since 2020-10-28
 */
@Slf4j
@Api(tags = "WP-账号配置",description = "WP-账号配置")
@RestController
@RequestMapping("/wp/tblWpAccountConfig")
public class TblWpAccountConfigController extends MergeApiExcelController<TblWpAccountConfig, Map<String, Object>> {

    @Value("${system.export-root.path}")
    private String exportRootPath;

    @Resource
    private ITblWpAccountConfigService tblWpAccountConfigService;

    @Override
    public IBaseService init() {
        return tblWpAccountConfigService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblWpAccountConfig.class;
    }

    @Override
    protected void setFootModel(Map<String, Object> result, Map<String, Object> model) throws ServiceException {
        TblWpAccountConfig foot=tblWpAccountConfigService.getFoot(model);
        result.put("foot", foot==null? getModelClass():foot);
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model, "supplierNo", "supplierName");
        super.setParamsModel(model);
    }

    @Override
    public String verifyImportObject(TblWpAccountConfig tblWpAccountConfig, AnalysisContext analysisContext) {
        String verify = verify(tblWpAccountConfig);
        if(StringUtils.isNotEmpty(verify)){
            return verify;
        }
        return super.verifyImportObject(tblWpAccountConfig, analysisContext);
    }

    private String verify(TblWpAccountConfig detail) {
        try{
            String website = detail.getWebsite();
            if("供应商管理平台".equals(website)){
                String password = detail.getPassword();
                String s = Base64.getEncoder().encodeToString(password.getBytes());
                detail.setPassword(s);
            }
        } catch (Exception ex) {
            log.error("verify error",ex);
            return ReturnStatusEnum.REQUEST_ABNORMAL.getText();
        }

        return null;
    }

    /*@Override
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> params = buildParam(request);
        setParamsModel(params);
        Object fileName = "WP-账号配置";
        String path = exportRootPath + System.currentTimeMillis() + "/";
        CsvExportUtil.createCsv()
                .headClass(getModelClass())
                .fileName(fileName.toString())
                .model(CsvExportBuilder.MULTI_ZIP_MODEL)
                .parentPath(path)
                .csvDatas(new DateSpiltCsvDatas(tblWpAccountConfigService, params))
                .export(response);
    }*/

    @Override
    protected ObjectDataResponse getPageList(HttpServletRequest request, @RequestBody BasePager<Map<String, Object>> model) throws ServiceException {
        ObjectDataResponse<Map> list = super.getPageList(request, model);
        Map data = list.getData();
        List<TblWpAccountConfig> rows = (List<TblWpAccountConfig>) data.get("rows");
        for(TblWpAccountConfig row :rows){
            String website = row.getWebsite();
            if("供应商管理平台".equals(website)){
                row.setPassword("******");
            }
        }
        data.put("rows",rows);
        list.setData(data);
        return list;
    }

    @Override
    protected void saveData(List<TblWpAccountConfig> datas) throws ServiceException {
        for(TblWpAccountConfig data : datas){
            Map<String,Object> map = new HashMap<>(16);
            map.put("loginAccount",data.getLoginAccount());
            List<TblWpAccountConfig> list = tblWpAccountConfigService.selectByParam(map);
            TblWpAccountConfig config = null;
            if(list!=null && !list.isEmpty()){
                config = list.get(0);
            }
            setDefualt(data);
            tblWpAccountConfigService.save(config,data);
        }
    }

    @Override
    protected List<TblWpAccountConfig> getExportDate(Map<String, Object> params) throws ServiceException {
        List<TblWpAccountConfig> list = super.getExportDate(params);
        for(TblWpAccountConfig row :list){
            String website = row.getWebsite();
            if("供应商管理平台".equals(website)){
                row.setPassword("******");
            }
        }
        return list;
    }
}

